import request from '@/utils/request'
import method from '@/utils/config'

const prefix = '/crm/leads'

const leadsApi = {
  // 分页查询线索数据
  listByParamsForTable: prefix + '/listByParamsForTable',
  // 根据id批量删除线索
  deleteBatchByIds: prefix + '/deleteBatchByIds',
  // 根据主键有选择性更新记录
  updateByPrimaryKeySelective: prefix + '/updateByPrimaryKeySelective',
  // 插入一条线索，返回主键
  saveHasKey: prefix + '/saveHasKey',
  // 根据id查询线索
  selectByPrimaryKey: prefix + '/selectByPrimaryKey',
  // 线索转换为客户
  transfer: prefix + '/transfer',
  // 变更线索负责人
  changeOwnerUser: prefix + '/changeOwnerUser'
}

/**
 * 分页查询线索数据
 * @param queryParams 查询对象
 * @returns {*}
 */
export function listByParamsForTable(queryParams) {
  return request({
    url: leadsApi.listByParamsForTable,
    method: method.POST,
    data: {
      page: queryParams.page,
      limit: queryParams.limit,
      search: queryParams.search
    }
  })
}

/**
 * 插入一条线索，返回主键
 * @param data 线索对象
 * @returns {*}
 */
export function saveHasKey(data) {
  return request({
    url: leadsApi.saveHasKey,
    method: method.POST,
    data
  })
}

/**
 * 根据id批量删除线索
 * @param ids id集合
 * @returns {*}
 */
export function deleteBatchByIds(ids) {
  return request({
    url: leadsApi.deleteBatchByIds + `/${ids}`,
    method: method.DELETE
  })
}

/**
 * 根据主键有选择性更新线索
 * @param data 线索对象
 * @returns {*}
 */
export function updateByPrimaryKeySelective(data) {
  return request({
    url: leadsApi.updateByPrimaryKeySelective,
    method: method.PUT,
    data
  })
}

/**
 * 根据id查询线索
 * @param id 线索id
 * @returns {*}
 */
export function selectByPrimaryKey(id) {
  return request({
    url: leadsApi.selectByPrimaryKey,
    method: method.GET,
    params: {
      id
    }
  })
}

/**
 * 线索转换为客户
 * @param ids 线索id
 * @returns {*}
 */
export function transfer(ids) {
  return request({
    url: leadsApi.transfer,
    method: method.PUT,
    params: {
      ids
    }
  })
}

/**
 * 修改线索负责人
 * @param data 负责人修改对象
 * @returns {AxiosPromise}
 */
export function changeOwnerUser(data) {
  return request({
    url: leadsApi.changeOwnerUser,
    method: method.PUT,
    data: {
      id: data.id,
      ownerId: data.ownerId,
      ownerUserName: data.ownerUserName,
      changeType: data.changeType,
      transferType: data.transferType,
      power: data.power
    }
  })
}
